<!-- modal模板，内置slot，可随意替换，直接引用，不需要再新建页面 -->
<template>
  <transition name="modal-fade">
    <div class="modal-backdrop" @click="close" v-show="show"  >
      <div class="ordermodal" @click.stop role="dialog" aria-labelledby="modalTile" arial-describedby="modalDescription" >
        <div class="modal-header" id="modalTitle">
        <slot name="header">
            {{title}}
            <a-icon type="close-circle" class="modal-header-title" :style="{ fontSize: '16px', color: '#111111'}" @click="close" aria-label="Close modal"/>
        </slot>
      </div>
      <div class="modal-body" id="modalDescription">
        <div class='modelHeight' >
            <div class="formclass">
              <div class="p-class">
                <p>您将提交如下交易信息，您可修改资产权重与资金组合权重。</p>
              </div>
              <div class="ordertables">
                <a-form layout="inline">
                         <a-form-item label="股票:">
                           <InputNumber width="80px"  @getType="getTypes" type='Acount' :max='100' :disabled="stockTableData.length<1" v-model="gpNumber"></InputNumber>
                         </a-form-item>

                         <a-form-item label="债券:">
                           <InputNumber width="80px" @getType="getTypes"  type='Acount' :max='100' :disabled="bondTableData.length<1" v-model="zqNumber" ></InputNumber>
                        </a-form-item>

                    <br/>
                    <a-checkbox  :defaultChecked="isOnChangeQZ" @change="onChangeQZ">资产权重值跟随基金权重调整变化</a-checkbox>
                   <div class="span-class">
                     <span v-bind:class="{'zx-errorInfo':errorRule1}" id='tswz1'>*各类资产权重值之和应为100%</span>

                   </div>
                    <el-divider></el-divider>
                    <div class="orderleft">
                      <div class="order-shang">
                        <span ><strong>股票型组合</strong></span>
                          <div class="ordertable">
                            <a-table v-show="stockTableData.length>0" :columns="stockcolumns" :dataSource="stockTableData" :pagination='false' @change="handleChange" bordered>
                              <template slot="stockoperation" slot-scope="text, record">
                                <InputNumber @getType="getTypes" type='Table' :isOnChangeQZ='isOnChangeQZ' v-model="record.countQz" :max="100"  :disabled="record.countQz===0" width="75px" ></InputNumber>
                              </template>
                            </a-table>
                            <!-- 金权重值之和应为该类资产权重值 -->
                            <div class="aa" >
                              
                              <span v-bind:class="{'zx-errorInfo':errorRule2}" id='tswz2'>*各基金权重值之和应为该类资产权重值</span>
                            </div>
                            <span class="orderNoData" v-show="!stockTableData.length>0">无构建基金组合</span>
                          </div>
                      </div>

                    </div>
                    <div class="orderright">
                      <div class="order-shang">
                        <span><strong>债券型组合</strong></span>
                        <div class="ordertable">
                          <a-table v-show="bondTableData.length>0" :columns="bondcolumns" :dataSource="bondTableData" :pagination='false' @change="handleChange" bordered>
                            <template slot="bondoperation" slot-scope="text, record">
                              <InputNumber @getType="getTypes" type='Table' :isOnChangeQZ='isOnChangeQZ' v-model="record.countQz" :max="100"   :disabled="record.countQz===0"  width="75px" ></InputNumber>
                            </template>
                          </a-table>
                          <!-- 金权重值之和应为该类资产权重值 -->
                          <div class="aa" >
                      
                              <span v-bind:class="{'zx-errorInfo':errorRule3}" id='tswz3'>*各基金权重值之和应为该类资产权重值</span>
                          </div>
                                    <span class="orderNoData" v-show="!bondTableData.length>0">无构建基金组合</span>
                        </div>
                      </div>

                    </div>
                </a-form>
              </div>

            </div>
        </div>
          <div class="order-tixing">

          </div>
          <!-- 金权重值之和应为该类资产权重值 -->
          <!-- <div class="aa" >
         
            <span v-bind:class="{'zx-errorInfo':errorRule2}" :style="{ fontSize: '15px',color:'#999'}">*各基金权重值之和应为该类资产权重值</span>
          </div> -->
          <div class="order-details">
            <span :style="{ fontSize: '13px', color: 'red' }">*</span><span>确认下单表示您已知晓相关风险。
              <a href="javascript:" @click="viewDetails" >{{showorder?'收起' : '查看详情'}}</a></span>
          </div>
          <div class="details-ts" v-if="showorder">
            <div style="text-align: left">
              <li><span style="color:red">风险提示</span></li>
              <li><span>尊敬的客户：</span></li>
              <li><span>
                在您获得本资产配置平台系统结论并用于实际投资时，可能会获得较高的投资收益，
                但同时也可能存在着较大的证券投资风险。请务必仔细阅读本系统平台的使用说明。
                我们将尽可能采取有效措施保护客户资料和其他各项活动的安全，本着对您负责的态度，
                公司郑重提示，在使用时，存在且不限于以下风险：
              </span></li>
              <li><span>1、资产配置平台系统仅是建立在基础数据（数据来源包括：市场公开数据及本公司外购数据源）平台上的一个页面，它同样具有电子化系统的使用风险。</span></li>
              <li><span>2、以下原因都可能造成您使用资产配置系统时出现中断、停顿、延迟和显示失败等情况</span></li>
              <li><span>1）电脑病毒、黑客入侵、计算机软硬件故障；</span></li>
              <li><span>2）您交易使用的电脑终端与行情、交易通讯线路发生故障；</span></li>
              <li><span>3）其他因网络或您的电脑终端可能出现的故障及其他不可测因素；</span></li>
              <li><span>4）因软件关闭、关机、繁忙或网络中断等其他原因出现延迟、中断、停顿或数据不完全，甚至出现系统故障，从而使系统显示出现延迟、中断、遗失、无法触发、数据错误</span></li>
              <li><span>3、软件适配风险。您的计算机配置、性能或软件系统与所提供的交易系统不匹配，无法及时显示结果的风险。</span></li>
              <li><span>4、政策变化风险。由于相关政策变化导致的风险。</span></li>
              <li><span>5、证券监管机构确定的其他风险。</span></li>
              <li><span>6、不可抗力及其他可能存在的风险。</span></li>
              <li><span>7、任何系统的使用都是建立在专属密码登录的前提下，凡您使用专属账号及密码进行的交易、查询及其他行为，均视为您本人的行为，请妥善保管自己的密码，由于您管理不善造成的密码泄露所带来的损失，公司不承担任何责任</span></li>
            </div>
          </div>
          <div class="roder-footer">
            <slot name="footer">
              <el-button size="mini" type="info" round class="btn-close"  @click="resetWeight" >权重归一</el-button>
              <el-button size="mini" type="info" round class="btn-close" @click="reset" >重置</el-button>
              <el-button size="mini" type="info" round class="btn-close" @click="close" >取消</el-button>
              <el-button size="mini" type="danger" round class="btn-confirm" @click="confirm">确认下单</el-button>
              <!-- <button type="button" class="btn-green" @click="close" aria-label="Close modal">关闭</button> -->
            </slot>
          </div>
      </div>



      </div>
    </div>
  </transition>

</template>
<script>
 import '@/style/orderModal.css'
  import '@/style/modal.css'
  import InputNumber from '@/components/utiljs/inputNumber'
export default {
   name: 'orderModal',
  provide(){
    return {
      instance:this
    }
  },
   components : {
    InputNumber
   },
    // props: ['show'],
    props:{
      orderData:Object,
      typeValue: {
          type: String,
          default: ""
      },
      // 是否显示
    show: {
      type: Boolean,
      default: false,
    },


  },
  watch:{
    orderData:{
      handler(val){
        this.doOrderData(val);
      },
      deep:true
    },
    gpNumber(val){
      console.log(this.evType)
       if(this.evType=='Acount'){
         if(val==0){
            for (let item of this.bondTableData){
                 item.latestSize = 0
              }
         }else {
                   let newArr = []
                    let ngpc = 0
                    for (let i = 0; i < this.gqqzList.length; i++) {
                        ngpc += Number((Number(this.gqqzList[i]) * parseFloat(val)).toFixed(2))
                        let num = (Number(this.gqqzList[i]) * parseFloat(val)).toFixed(2)
                        newArr.push(Number(num))
                    }
                  console.log(ngpc)
                  // let f_count = Number((parseFloat(val) / 5).toFixed(2));
                  let gpc=Number(parseFloat(val)) - Number(ngpc.toFixed(2))
                  gpc=Number(gpc.toFixed(2))
                  console.log(newArr)
                  this.stockTableData.forEach((item,index)=>{
                    item.countQz=newArr[index]
                      if(index==0){
                          if(gpc>0){
                              item.countQz=Number((Number(item.countQz)+gpc).toFixed(2))
                          }else if(gpc<0){
                            item.countQz=Number((Number(item.countQz)+gpc).toFixed(2))
                          }else {
                            item.countQz=Number(item.countQz.toFixed(2))
                          }

                      }else{
                        item.countQz=Number(item.countQz)
                      }

                  })
              }

        }
    },
    zqNumber(val){
      console.log(this.evType)
          if(this.evType=='Acount'){
            if(val==0){
                 for (let item of this.bondTableData){
                 item.latestSize = 0
              }
            }else {
                 let newArr = []
                    let ngpc = 0
                    for (let i = 0; i < this.zqqzList.length; i++) {
                        ngpc += Number((Number(this.zqqzList[i]) * parseFloat(val)).toFixed(2))
                        let num = (Number(this.zqqzList[i]) * parseFloat(val)).toFixed(2)
                        newArr.push(Number(num))
                    }

            // let f_count = Number((parseFloat(val) / 5).toFixed(2));
              let zqc=Number(parseFloat(val)) - Number(ngpc.toFixed(2))
                      zqc=Number(zqc.toFixed(2))
                      this.bondTableData.forEach((item,index)=>{
                        item.countQz=newArr[index]
                          if(index==0){
                              if(zqc>0){
                      
                                  item.countQz=Number((Number(item.countQz)+zqc).toFixed(2))
                              }else if(zqc<0){
                            
                                item.countQz=Number((Number(item.countQz)+zqc).toFixed(2))
                              }else  {
                               
                                item.countQz=Number(item.countQz)
                              }

                          }else{
                            item.countQz=Number(item.countQz)
                          }

                      })
            }
                  
          }

    },
    stockTableData:{
      handler(val){
        this.checkRule2(val,this.bondTableData)
         this.gpCount(val)
      },
      deep:true,
      immediate:true
    },
    bondTableData:{
      handler(val){
        this.checkRule2(this.stockTableData,val)
         this.ZQCount(val)
      },
      deep:true,
      immediate:true
    }
  },
    data () {
       return {
         isOnChangeQZ:true,// 是否勾选权重
         gqqzList: ['0.2', '0.2', '0.2', '0.2', '0.2'],
         zqqzList: ['0.2', '0.2', '0.2', '0.2', '0.2'],
         evType:null,
         numberState:0,
         isUserCustom:true,
         errorRule2:false,
         errorRule3:false,
         gpNumber:0,
         zqNumber:0,
         InputNumber:1,
         showorder:false,
         title:"一键下单",
         //股票table
         stockTableData:[],
         //债券table
         bondTableData:[],
         //现金table
         windTableData:[
           { fundCode:'070008.OF',
             fundName:'嘉实货币基金',
             quanzhong:'10'
            }
         ],
        }
      },
      computed:{
        errorRule1(){
          this.checkRule2(this.stockTableData,this.bondTableData);
          return (parseFloat(this.gpNumber) + parseFloat(this.zqNumber))!==100 && this.isUserCustom;
        },
        stockcolumns(){
         const stockcolumns = [
          {
            title: '基金代码',
            align:'center',
            dataIndex: 'fundCode',
            key: 'fundCode',
          },
          {
            title: '基金名称',
            align:'center',
            dataIndex: 'fundName',
            key:'fundName',
          },
           {
             title: '基金权重',
             dataIndex: 'stockoperation',
             scopedSlots: { customRender: 'stockoperation' },
             width:130,
             align:"center"
           }
        ];
          return stockcolumns;
        },
      bondcolumns(){

       const bondcolumns = [
        {
          title: '代码',
          align:'center',
          dataIndex: 'fundCode',
          key: 'fundCode',

        },
        {
          title: '名称',
          align:'center',
          dataIndex: 'fundName',
          key:'fundName',

        },
         {
           title: '基金权重',
           dataIndex: 'bondoperation',
           scopedSlots: { customRender: 'bondoperation' },
           width:130,
           align:"center"
         }
      ];
        return bondcolumns;
      },
      windcolumns(){

       const windcolumns = [
        {
          title: '代码',
          align:'center',
          dataIndex: 'fundCode',
          key: 'fundCode',

        },
        {
          title: '名称',
          align:'center',
          dataIndex: 'fundName',
          key:'fundName',

        },
        {
          title: '权重(%)',
          align:'center',
          dataIndex: 'quanzhong',
          key: 'quanzhong',
        },
      ];
        return windcolumns;
      },
    },
      methods: {
                //重置权重
        resetWeight(){

          if(this.gpNumber==0&&this.zqNumber==0){
              this.$message.error("股票和债券不能同时为0！");
                return false
          }
          // (parseFloat(this.gpNumber) + parseFloat(this.zqNumber) +parseFloat(this.spNumber)+parseFloat(this.xjNumber))!==100 && this.isUserCustom;
           let AcountQz=(parseFloat(this.gpNumber) + parseFloat(this.zqNumber))/100
           this.gpNumber=(this.gpNumber/AcountQz).toFixed(2)
           this.zqNumber=(this.zqNumber/AcountQz).toFixed(2)
          
        
           let zc=100-(Number(this.gpNumber)+Number(this.zqNumber))
     
           if(zc<0){
                // 小于0负数
             
                this.gpNumber=Number((Number(this.gpNumber)+zc).toFixed(2))
              }else if(zc>0){
                 this.gpNumber=Number((Number(this.gpNumber)+zc).toFixed(2))
              }
           
          if(this.gpNumber==0){
            for (let item of this.stockTableData){
                item.countQz=0
             }
          } else {

          
          // 计算误差 股票误差
           let total=0;// 总数
           let Gptotal=0;//分配后的总数 用来计算误差 和总数对比
           let PID=0;// 计算系数
            // 股票均分
            for (let item of this.stockTableData){
                item.countQz=parseFloat(item.countQz)
                total+=item.countQz
             }
             PID=total/this.gpNumber
             //根据系数分配值
              this.stockTableData.forEach((item,index)=>{
             item.countQz=(item.countQz/PID).toFixed(2)
              Gptotal+=Number(item.countQz)   
           })

            let gpc = (this.gpNumber-Gptotal)
            gpc=Number(gpc.toFixed(2))

          //  计算误差 分配到第一项
            this.stockTableData.forEach((item,index)=>{
                if(index==0){
                    if(gpc<0){
                      // 负数
                      item.countQz =Number((Number(item.countQz)+gpc).toFixed(2))
                    }else if(gpc>0) {
                        item.countQz =Number((Number(item.countQz)+ Number(gpc)).toFixed(2))
                    } else {
                       item.countQz =Number((Number(item.countQz)).toFixed(2))
                    }                  
                  }
            })
           }
           if(this.zqNumber==0){
             for (let item of this.bondTableData){
          
                ZQtocal=0
             }

           }else {

           
            // 计算误差 误差
           let ZQtocal=0;
           let ZQfzs=0
           let ZQPID=0;
           // 债券均分
            for (let item of this.bondTableData){
                item.countQz=parseFloat(item.countQz)
                ZQtocal+=item.countQz
             }
             ZQPID=ZQtocal/this.zqNumber
             //根据系数分配值
              this.bondTableData.forEach((item,index)=>{
             item.countQz=(item.countQz/ZQPID).toFixed(2)
              ZQfzs+=Number(item.countQz)   
           })
            let zqc = (this.zqNumber-ZQfzs)
            zqc=Number(zqc.toFixed(2))
 
          //  计算误差 分配到第一项
            this.bondTableData.forEach((item,index)=>{
                if(index==0){
                    if(zqc<0){
                      // 负数
                      item.countQz =Number((Number(item.countQz)+zqc).toFixed(2))
                    }else if(zqc>0) {
                        item.countQz =Number((Number(item.countQz)+ Number(zqc)).toFixed(2))
                    } else {
                      item.countQz =Number((Number(item.countQz)).toFixed(2))
                    }                  
                  }

         
            })
           }
        },
        // 获取事件类型
        getTypes(val){
        
          this.evType=val
        },
        // 勾选框
       onChangeQZ(e){
         
         this.isOnChangeQZ=e.target.checked
       },
        // 股票权重计算
        gpCount(data){ 
          if(this.isOnChangeQZ&&this.evType=='Table'){
            this.ischangeTB=true
            let Acount=0;
         data.forEach(item=>{
           Acount+=parseFloat(item.countQz)
         })
          // 自定义后各指标项占比
                let list = [];
                data.forEach(item => {
                    list.push((item.countQz / Acount).toFixed(2))
                })
                let qzzs = 0
                for (let i = 0; i < list.length; i++) {
                    qzzs += Number(list[i])
                }
                let wc = 1 - qzzs
                list[0] = (Number(list[0]) + Number(wc)).toFixed(2)
                this.gqqzList = list
          

          return this.gpNumber=Number(Acount.toFixed(2))
          }      
        },
        ZQCount(data){
     
          if(this.isOnChangeQZ&&this.evType=='Table'){
                      let Acount=0;
                  data.forEach(item=>{
                    Acount+=Number(item.countQz)
                  })

                   // 债券自定义后占比
                let list = [];
                data.forEach(item => {
                    list.push((item.countQz / Acount).toFixed(2))
                })
                console.log(list)
                let qzzs = 0
                for (let i = 0; i < list.length; i++) {
                    qzzs += Number(list[i])
                }
                let wc = 1 - qzzs
                list[0] = (Number(list[0]) + Number(wc)).toFixed(2)
                this.zqqzList = list
                console.log(this.zqqzList)
                 
                    return this.zqNumber=Acount.toFixed(2)
                    }   
        },
      



        checkRule2(stockTable,bondTableData){
          let scount = 0;
          if(stockTable.length>0){
            for (let item of stockTable){
              scount += parseFloat(item.countQz);
            }
          }

          let wcount = 0;

          if(bondTableData.length>0){
            for (let item of bondTableData){
              wcount += parseFloat(item.countQz);
            }
          }
          if(parseFloat(scount).toFixed(2) === parseFloat(this.gpNumber).toFixed(2)){
            this.errorRule2 = false;
          }else {
            this.errorRule2 = true;
          }
           if( parseFloat(wcount).toFixed(2) === parseFloat(this.zqNumber).toFixed(2)){
            this.errorRule3 = false;
          }else {
            this.errorRule3 = true;
          }
        },
        doOrderData(data){
          this.isUserCustom = false;
          let gpData = data.stockTableData,zqData = data.bondTableData;

          if(gpData.length>0 && zqData.length>0){
            this.gpNumber = 50;
            this.zqNumber = 50;
          }else{
            if(gpData.length<1){
              this.gpNumber = 0;
              this.zqNumber = 100;
            }
            if(zqData.length<1){
              this.zqNumber = 0;
              this.gpNumber = 100;
            }
          }
          // 计算差值
           let acountwc=100-(this.gpNumber+this.zqNumber)
              if(acountwc<0){
                // 小于0负数
                this.gpNumber=Number((this.gpNumber+acountwc).toFixed(2))
              }else if(acountwc>0){
                 this.gpNumber=Number((this.gpNumber+acountwc).toFixed(2))
              }
          for (let item of zqData){
            item["countQz"] = parseFloat(this.zqNumber / 5).toFixed(2);
          }
          for (let item of gpData){
            item["countQz"] =  parseFloat(this.gpNumber / 5).toFixed(2);
          }
          this.stockTableData = JSON.parse(JSON.stringify(gpData));
          this.bondTableData = JSON.parse(JSON.stringify(zqData));
          // 默认初始进来设置table值等于股票总值
                  let f_count = Number(parseFloat(this.gpNumber) / 5).toFixed(2);
                  let gpc=Number(parseFloat(this.gpNumber))-(f_count*5)
                  gpc=Number(gpc.toFixed(2))
                  this.stockTableData.forEach((item,index)=>{
                      if(index==0){
                          if(gpc>0){
                              item.latestSize=Number(f_count)+gpc
                          }else if(gpc<0){
                            item.latestSize=Number(f_count)-gpc
                          }

                      }else{
                        item.latestSize=Number(f_count)
                      }

                  })

                  // 债券
                  let f_count1 = Number(parseFloat(this.zqNumber) / 5).toFixed(2);
                  let zqc=Number(parseFloat(this.zqNumber))-(f_count1*5)
                  zqc=Number(zqc.toFixed(2))
                  this.bondTableData.forEach((item,index)=>{
                      if(index==0){
                          if(zqc>0){
                              item.latestSize=Number(f_count1)+zqc
                          }else if(zqc<0){
                            item.latestSize=Number((Number(f_count1)+zqc).toFixed(2))
                          }
                      }else{
                        item.latestSize=Number(f_count1)
                      }
                  })
        },
        // Change事件
        handleChange(){},
        onChange(){},
        // 确定按钮事件
        confirm:function() {
          if(!this.errorRule2&&!this.errorRule1){
            this.$emit('confirm',this.typeValue);
          }else{
            let errorMsg = [];
            if(this.errorRule1){
              errorMsg.push('各类资产权重值之和应为100%')
            }
            if(this.errorRule2){
              errorMsg.push('各基金权重值之和应为该类资产权重值');
            }
            this.$message.error(errorMsg.join(","));
          }
        },
        close: function () {
           this.gqqzList=['0.2', '0.2', '0.2', '0.2', '0.2']
         this.zqqzList=['0.2', '0.2', '0.2', '0.2', '0.2']
          this.$emit('close');

        },
        reset:function(){
          this.doOrderData(this.orderData);
          this.$emit('reset');
        },
        viewDetails(){
          this.showorder=!this.showorder;
        },
        unification(){
          this.doOrderData(this.orderData);
        },
      },
      beforeDestroy () {

      }
    }
</script>
